package jgl.vajra.kingkong.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.databinding.ViewInputLoginInfoBinding;
import jgl.vajra.kingkong.listener.VerifyCodeClickListener;
import jgl.vajra.kingkong.widget.base.BaseCustomView;
import jgl.vajra.kingkong.widget.interfaces.VerifyCodeListener;
import jgl.vajra.kingkong.widget.utils.ToastUtils;

import com.orhanobut.logger.Logger;

/**
 * Created by Administrator on 2019/11/4.
 */

public class InputLoginInfoView extends BaseCustomView<ViewInputLoginInfoBinding> {
    String text = "";
    private int inputLoginInfoType;
    private VerifyCodeClickListener verifyCodeClickListener;

    public InputLoginInfoView(Context context) {
        super(context);
    }

    public InputLoginInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void initEvent(Context context) {
        mBinding.tvText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                mBinding.viewLine.setBackgroundResource(!hasFocus ? R.color.gray_e8 : R.color.orange_f183);
            }
        });
/*
        mBinding.tvText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mBinding.viewLine.setBackgroundResource(TextUtils.isEmpty(getInputText()) ? R.color.gray_e8 : R.color.orange_f183);
            }
        });
*/
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_input_login_info;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        mBinding.setText(text);
        mBinding.setView(this);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputLoginInfoView);
        /*int inputLoginInfoImageRes = typedArray.getResourceId(R.styleable.InputLoginInfoView_InputLoginInfoImageRes, R.mipmap.login_phone);
        mBinding.ivImage.setBackgroundResource(inputLoginInfoImageRes);*/
        String inputLoginInfoTextHint = typedArray.getString(R.styleable.InputLoginInfoView_InputLoginInfoTextHint);
        inputLoginInfoType = typedArray.getInteger(R.styleable.InputLoginInfoView_InputLoginInfoType, 0);
        mBinding.tvText.setHint(inputLoginInfoTextHint);
        typedArray.recycle();
        mBinding.setInputType(inputLoginInfoType);
        Logger.d("inputLoginInfoType==" + inputLoginInfoType);
        switch (inputLoginInfoType) {
            case 0:
                mBinding.llCheckCountryCode.setVisibility(VISIBLE);
                mBinding.tvText.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 1:
                //mBinding.tvText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                // mBinding.ivPasswordStatus.setVisibility(VISIBLE);
                mBinding.tvText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                break;
            case 2:
                mBinding.tvText.setInputType(InputType.TYPE_CLASS_NUMBER);
                mBinding.verifyCodeView.setVisibility(VISIBLE);
                break;
            case 3:
                mBinding.tvText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mBinding.tvResetPassword.setVisibility(VISIBLE);
                //   mBinding.tvText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                break;


        }

    }

    public void setResetPasswordVisibility(int visibility) {
        mBinding.tvResetPassword.setVisibility(visibility);
    }

    public String getInputText() {
        return mBinding.tvText.getText().toString().trim();
    }

    boolean isLookPassword = false;

   /* public void onPasswordCheck() {
        mBinding.ivPasswordStatus.setSelected(isLookPassword);
        if (!isLookPassword) {
            mBinding.tvText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            mBinding.tvText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        mBinding.tvText.setSelection(mBinding.tvText.length());
        isLookPassword = !isLookPassword;

    }*/

    public void onResetPassword() {
        if (verifyCodeClickListener != null) {
            verifyCodeClickListener.onClickVerifyCode();
        }

    }

    public void onCheckCountryPhoneCode(){

    }

    public void onVerifyCode() {
        if (verifyCodeClickListener != null) {
            verifyCodeClickListener.onClickVerifyCode();
        }

    }

    public void getVerifyCode(String phone) {
        mBinding.tvText.setFocusable(true);
        mBinding.tvText.requestFocus();
        mBinding.verifyCodeView.getVerifyCode(phone);


    }

    public void setVerifyCodeListener(VerifyCodeListener listener) {
        mBinding.verifyCodeView.setVerifyCodeListener(listener);
    }

    public void setVerifyCodeClickListener(VerifyCodeClickListener listener) {
        this.verifyCodeClickListener = listener;
    }


    public boolean isVerifyPhone(String inputPhone) {
        if (TextUtils.isEmpty(inputPhone)) {
            ToastUtils.showShort("请输入手机号码");
            return false;
        }
       /* if (!RegularUtils.isMobileSimple(inputPhone)) {
            ToastUtils.showShort("请输入正确的手机号码");
            return false;
        }*/
        return true;
    }


    public void setInputType(int inputType) {
        mBinding.tvText.setInputType(inputType);
    }

    public void setInputText(String text) {
        mBinding.tvText.setText(text);
    }

    public EditText getEditText() {
        return mBinding.tvText;
    }

    public String getCountryPhoneCode() {
        return mBinding.tvCountryPhoneCode.getText().toString();
    }


}
